package com.heo.protocol.packet;

import org.apache.log4j.Logger;

import com.heo.util.ByteHelper;

public class FirmwareDownloadReq extends BasePacket
{
  private int BODY_LENGTH = 6;// 包体长3个字节
  private static final String PACKET_NAME = "[固件下载请求包]";

  private int startIndex;// 起始位置，4个字节
  private int downLength;// 下载文件长度，2个字节

  private static Logger log = Logger.getLogger(FirmwareDownloadReq.class);

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public FirmwareDownloadReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  public FirmwareDownloadReq() throws Exception
  {
    super();
    super.commandId = DeviceCommand.FIRMWARE_DOWNLOAD_REQ;
    super.sequenceId = 0x01;
    super.totalLength = HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(ByteHelper.intToByteArray(startIndex), ByteHelper.shortToByteArray((short)downLength), null, null, null, null, null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    startIndex = ByteHelper.byteToInt(packetBody, 0, 4);
    downLength = ByteHelper.byteToShort(packetBody, 4, 2);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {

  }

  public int getStartIndex()
  {
    return startIndex;
  }

  public void setStartIndex(int startIndex)
  {
    this.startIndex = startIndex;
  }

  public int getDownLength()
  {
    return downLength;
  }

  public void setDownLength(int downLength)
  {
    this.downLength = downLength;
  }

  
}
